//
//  LDMyResumeCellExperienceInAll.swift
//  DingDongZP
//
//  Created by LeMac on 16/12/27.
//  Copyright © 2016年 51ledian.com. All rights reserved.
//

import UIKit

class LDMyResumeCellExperienceInAll: LDMyResumeBaseCellInAll {


    let rowsViewTag = 555
    var cellH:CGFloat = 0
    var type = LDMyResumeBaseCellInAllType.Defalut

    lazy var voiceBtn:LDMyResumeBaseVoiceBtn = {
       
        let btn = LDMyResumeBaseVoiceBtn()
        return btn
    }()
    
    var vocieBtnClosure:((AnyObject)->())?
    
    override func updateView() {
        
        for item in bgView!.subviews {
            
            if item is UIButton == false {
                
                item.removeFromSuperview()
            }
        }
        
        addSubview(voiceBtn)
        addSubview(noticeLab)
        
        for (i,_) in model!.workexperience!.enumerate() {
            
            let newRowV =  LDMyResumeCellExperienceInAll_Row()
            newRowV.tag = rowsViewTag + i
            bgView!.addSubview(newRowV)
        
            guard let rowModel = model?.workexperience[i] else { return }
            newRowV.model = rowModel
        }
        
    
        for (i,_) in model!.workexperience!.enumerate() {
            

            let views = viewWithTag(rowsViewTag+i) as! LDMyResumeCellExperienceInAll_Row
            let attrH = views.getAttrHeight() + views.getLabsTagHeight() + 80
            
            if i < 1 {
                
                views.snp_makeConstraints(closure: { (make) in
                    
                    make.top.equalTo(LDMargin_V*3)
                    make.left.equalTo(LDMargin_V*3)
                    make.right.equalTo(0)
                    make.height.equalTo(attrH)
                    make.width.equalTo(textViewWidth)
                })
                
            } else {
                
                let preView = viewWithTag(rowsViewTag+i-1) as! LDMyResumeCellExperienceInAll_Row
                views.snp_makeConstraints(closure: { (make) in
                    make.top.equalTo(preView.snp_bottom)
                    make.left.equalTo(LDMargin_V*3)
                    make.right.equalTo(0)
                    make.height.equalTo(attrH)
                    make.width.equalTo(textViewWidth)
                })
            }
            
            
            cellH += attrH
            
            
            if i == model!.workexperience.count - 1 {
                
                let lineH = UIView()
                lineH.backgroundColor = LDLineColorLightGray
                bgView!.addSubview(lineH)
                lineH.snp_makeConstraints { (make) in
                    
                    make.top.equalTo(views.snp_bottom)
                    make.centerX.equalTo(views.dotView!)
                    make.bottom.equalTo(0)
                    make.width.equalTo(0.5)
                    
                }
            }
            
        }
        
        voiceBtn.snp_makeConstraints { (make) in
            
            make.left.equalTo(LDMargin_V*7)
            make.right.equalTo(-80)
            make.height.equalTo(28)
            make.bottom.equalTo(-LDMargin_V*2)
        }
        
        noticeLab.snp_makeConstraints { (make) in
            
            make.left.equalTo(85)
            make.centerY.equalTo(voiceBtn)
            make.height.equalTo(20)
        }
        
        
        voiceBtn.voiceBtn!.bk_whenTapped { [weak self] in
            
            if self?.vocieBtnClosure != nil {
                
                self?.vocieBtnClosure!(self!)
            }
        }
        
        bgView?.bringSubviewToFront(editBtn!)
        
        
        if model?.audioworkexperience_isset == "1" {
            
            voiceBtn.voiceBtn?.timeLab?.text = String(format: "%@'",model?.audioworkexperience_duration ?? "")
            noticeLab.hidden = true
            voiceBtn.voiceBtn?.hidden = false
            
        } else {
            
            voiceBtn.voiceBtn?.hidden = true
            noticeLab.hidden = false
        }
        
        //左边 细线
        

        
        
    }
    
    //cell 高度    
    override func getCellHeight(model: DDMyResumeModel) -> CGFloat {
        
        
        var cellHight:CGFloat = 0
        
        for (_,item) in model.workexperience.enumerate() {
            
            let attrTextH = String.getHeightFormAttr(LDFrame.AttrLineH, title: item.jobdesc ?? "", size: CGSizeMake(LDMyResumeCellExperienceInAll_Row.textViewWidth, 1000), font: LDFont.Font14)
            
            let tagList = item.jobstrenths.map({ return $0.tag ?? ""})
            let attrTagH = UILabel.getLabsAllHight(tagList, labH: 20, font: LDFont.Font13, originX: 23, marginX: 13, marginY: 13)
            
            let allH = attrTextH + attrTagH + 80
            cellHight += allH
            
        }
        
        if model.workexperience.count == 0 {
            
            return 20 + 50 + 50
        }
    
        return cellHight + 40 + 50 //底部语音btn
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupView()
    }
    
    func setupView() {
        
        title = "工作经历"
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
